package net.tngou.c4j.ml;

import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;

import net.tngou.c4j.ik.IKUtil;
import net.tngou.c4j.ik.WordFren;
import net.tngou.crawler.enums.MlTypeEnum;
import net.tngou.crawler.util.DateUtil;
import net.tngou.crawler.util.DigestMD;
import net.tngou.crawler.util.JsoupUtil;
import net.tngou.crawler.util.StringUtil;

/**
 * 模型
 * @author tngou@tngou.net
 *
 */
public class Model {
	private String key	;// 	 	K-V 对应的KEY 
	private String value;//         值
	private int isnull	;// 	 	是否为空 0-不为空 1-为空 
	private int length	;//	 	          长度 
	private int word	;// 	 	最少关键词 
	private int filterword	;//	 	过滤关键词 
	private int type	;//	 	类型：0-字符串 1-数字 2-时间 3-浮点型  4-文本
	private String[] loops;//指纹
	public Model(String value) {
		setData("text", value);
	}
	
	public Model(String key,String value) {
		setData(key, value);
	}
	private void setData(String key,String value) {
		this.setKey(key);
		this.setValue(value);
		this.setIsnull(_isnull());
		this.setLength(_length());
		int[] r = _word();
		this.setWord(r[0]);
		this.setFilterword(r[1]);
		this.setType(_type());
		this.setLoops(_loops());
	}
	
	private String[] _loops() {
		String[] r= {};
		switch (type) {
		case 0:
			String word = IKUtil.WordsFrem(this.value);
			String[] ws = StringUtil.split(word, " ");
			r= new String[ws.length];
			for (int i = 0; i < ws.length; i++) {
				r[i]=DigestMD.MD5To16(ws[i]);
			}
			break;
		case 1:
			r= new String[1];
			r[0]=DigestMD.MD5To16(this.value);
			break;
		case 2:
			r= new String[1];
			r[0]=DigestMD.MD5To16(this.value);
			break;
		case 3:
			r= new String[1];
			r[0]=DigestMD.MD5To16(this.value);
			break;
		case 4:
			String sentence = IKUtil.Sentence(this.value);
			String[] ss = StringUtil.split(sentence, " ");
			r= new String[ss.length];
			for (int i = 0; i < ss.length; i++) {
				r[i]=DigestMD.MD5To16(ss[i]);
			}
			break;
		default:
			r= new String[1];
			r[0]=DigestMD.MD5To16(this.value);
			break;
		}
		return r;
	}

	private int _isnull() {
		return StringUtil.isEmpty(this.value)?1:0;
	}
	
	private int _length() {
		return JsoupUtil.Text(value).length();
	}
	
	private int[] _word() {
		int[] r= {0,0};
		String text = JsoupUtil.Text(value);
		 List<WordFren> list = IKUtil.WordsFrem(text,Integer.MAX_VALUE);
		 for (WordFren wordFren : list) {
			switch (wordFren.getFilter()) {
			case 1:
				r[0]=r[0]+1;
				break;
			case -1:
				r[1]=r[1]+1;
				break;
			default:
				break;
			}
		}
		return r;
	}
	
	private int _type() {
		
		if(NumberUtils.isNumber(this.value)) {
			if(this.value.indexOf(".")>0)return MlTypeEnum.数字.ordinal();
			return MlTypeEnum.浮点型.ordinal();
		}
		if(DateUtil.parse(this.value)!=null)return MlTypeEnum.时间.ordinal();
		if(this.value.length()>256)return MlTypeEnum.文本.ordinal();
		return MlTypeEnum.字符串.ordinal();
	}
	
	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public int getIsnull() {
		return isnull;
	}

	public void setIsnull(int isnull) {
		this.isnull = isnull;
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public int getWord() {
		return word;
	}

	public void setWord(int word) {
		this.word = word;
	}

	public int getFilterword() {
		return filterword;
	}

	public void setFilterword(int filterword) {
		this.filterword = filterword;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public String[] getLoops() {
		return loops;
	}

	public void setLoops(String[] loops) {
		this.loops = loops;
	}
	

	
	public boolean equals(Model obj) {
		if(!getKey().equals(obj.getKey()))return false;
		if(getType()!=obj.getType())return false;
		String[] objlopps = obj.getLoops();
		if(objlopps.length!=loops.length)return false;
		for (int i = 0; i < objlopps.length; i++) {
			if(!loops[i].equals(objlopps[i]))return false;
		}
		return true;
	}
	
	
	/**
	 * 对比
	 * @param obj
	 * @return 0-100 %
	 */
	public int contrast(Model obj) {
		int contrast=0;
		if(!getKey().equals(obj.getKey()))return contrast;
		if(getType()!=obj.getType())return contrast;
		String[] objlopps = obj.getLoops();
		int length=objlopps.length>loops.length?objlopps.length:loops.length;
		int pt = 100/length;
		for (String oblps: objlopps) {
			for (String lp : loops) {
				if(lp.equals(oblps))contrast=contrast+pt;
			}
		}
		return contrast;	
	}
}
